﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;

namespace StairsModuleAndEdit.HYElevator.ShowForm
{
	public partial class SelectFloorForm : System.Windows.Forms.Form
	{
		public Dictionary<ViewPlan, bool> LevelStatus
		{
			get
			{
				return this.m_LevelStatus;
			}
		}

		public SelectFloorForm(List<ViewPlan> views)
		{
			this.m_Views = views;
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			List<string> list = new List<string>();
			for (int i = 0; i < this.m_Views.Count; i++)
			{
				ViewPlan viewPlan = this.m_Views[i];
				list.Add(viewPlan.Name);
			}
			this.ReadLayerCheckd(ref list);
			for (int j = 0; j < this.m_Views.Count; j++)
			{
				ViewPlan view = this.m_Views[j];
				this.chkList.Items.Add(view.Name);
				if (list.Find((string name) => name == view.ViewName) == null)
				{
					this.chkList.SetItemChecked(j, false);
				}
				else
				{
					this.chkList.SetItemChecked(j, true);
				}
			}
		}

		private void btnSelAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				this.chkList.SetItemChecked(i, true);
			}
		}

		private void btnSelNone_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				this.chkList.SetItemChecked(i, false);
			}
		}

		private void btnSelContrary_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				bool value = !this.chkList.GetItemChecked(i);
				this.chkList.SetItemChecked(i, value);
			}
		}

		private void SelectFloorForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			if (!string.IsNullOrEmpty(this.strFormCaption))
			{
				this.Text = this.strFormCaption;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			string userDataLocation = Product.UserDataLocation;
			string path = userDataLocation + "\\SetLayer.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			int x = base.Location.X;
			int y = base.Location.Y;
			streamWriter.WriteLine(x.ToString());
			streamWriter.WriteLine(y.ToString());
			int num = 0;
			for (int i = 0; i < this.chkList.Items.Count; i++)
			{
				bool itemChecked = this.chkList.GetItemChecked(i);
				string strLayerName = this.chkList.GetItemText(this.chkList.Items[i]);
				ViewPlan key = this.m_Views.Find((ViewPlan layer) => layer.ViewName == strLayerName);
				if (itemChecked)
				{
					num = 1;
					streamWriter.WriteLine(strLayerName);
				}
				if (num == 1)
				{
					this.m_LevelStatus.Add(key, itemChecked);
				}
			}
			streamWriter.Close();
			fileStream.Close();
			ViewPlan viewPlan = null;
			foreach (KeyValuePair<ViewPlan, bool> keyValuePair in this.m_LevelStatus)
			{
				if (keyValuePair.Value)
				{
					viewPlan = keyValuePair.Key;
				}
			}
			List<ViewPlan> list = new List<ViewPlan>();
			foreach (KeyValuePair<ViewPlan, bool> keyValuePair2 in this.m_LevelStatus)
			{
				if (keyValuePair2.Key.GenLevel.Elevation > viewPlan.GenLevel.Elevation)
				{
					list.Add(keyValuePair2.Key);
				}
			}
			if (list != null)
			{
				foreach (ViewPlan key2 in list)
				{
					this.m_LevelStatus.Remove(key2);
				}
			}
		}

		private void ReadLayerCheckd(ref List<string> SelectName)
		{
			string userDataLocation = Product.UserDataLocation;
			string path = userDataLocation + "\\SetLayer.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				return;
			}
			SelectName.Clear();
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			base.StartPosition = FormStartPosition.Manual;
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			for (;;)
			{
				string text = streamReader.ReadLine();
				if (text == null)
				{
					break;
				}
				SelectName.Add(text);
			}
			streamReader.Close();
			fileStream.Close();
		}

		private List<ViewPlan> m_Views = new List<ViewPlan>();

		public string strFormCaption;

		private Dictionary<ViewPlan, bool> m_LevelStatus = new Dictionary<ViewPlan, bool>();
	}
}
